// FireWallDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FireWall.h"
#include "FireWallDlg.h"
#include "../APIHook/APIHook.h"
#include "Process.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CFireWallDlg dialog




CFireWallDlg::CFireWallDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFireWallDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFireWallDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PROCESSLIST, m_ProcessList);
}

BEGIN_MESSAGE_MAP(CFireWallDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDCANCEL, &CFireWallDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDOK, &CFireWallDlg::OnBnClickedOk)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_NETCONTROL, &CFireWallDlg::OnBnClickedBtnNetcontrol)
END_MESSAGE_MAP()


// CFireWallDlg message handlers

BOOL CFireWallDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	CAPIHook::SetThisProcessFireWall(this->m_hWnd);
	CProcess::Initialize();
	CProcess::SetPostBack(this);
	CProcess::StartMonitor();

	BYTE i=0;
	m_ProcessList.InsertColumn(i++,"PID",LVCFMT_LEFT,50);
	m_ProcessList.InsertColumn(i++,"Path",LVCFMT_LEFT,280);
	m_ProcessList.InsertColumn(i++,"CRC",LVCFMT_LEFT,60);
	m_ProcessList.InsertColumn(i++,"IsConnected",LVCFMT_LEFT,40);
	m_ProcessList.InsertColumn(i++,"Protocol",LVCFMT_LEFT,40);



	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFireWallDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFireWallDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CFireWallDlg::OnBnClickedCancel()
{
	::MessageBoxW(NULL,L"",L"",MB_OK);
	CAPIHook::UnHookFun(FunNum,TRUE);
	//OnCancel();
}

void CFireWallDlg::OnBnClickedOk()
{
	::MessageBoxW(NULL,L"",L"",MB_OK);
	OnOK();
}

void CFireWallDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default

	CDialog::OnTimer(nIDEvent);
}

void CFireWallDlg::OnProcessEvents( ProcessEvents eEvents,LPVOID postBuf,DWORD bufSize )
{
	switch(eEvents)
	{
	case event_AddNew:
		{
			if (bufSize==sizeof(ProcInfo) && postBuf!=NULL)
			{
				AddProcessListItem(*(ProcInfo*)postBuf);
			}
			break;
		}
	case event_DelProc:
		{
			if (bufSize==sizeof(ProcInfo) && postBuf!=NULL)
			{
				DeleteProcessListItem(*(ProcInfo*)postBuf);
			}
			break;
		}
	}
}

void CFireWallDlg::AddProcessListItem( ProcInfo pi )
{
	char buf[MAX_PATH]={0};
	sprintf_s(buf,MAX_PATH,"%d",pi.pid);
	int nRet=m_ProcessList.InsertItem(0,buf);

	SecureZeroMemory(buf,MAX_PATH);
	sprintf_s(buf,MAX_PATH,"%s",pi.FilePath);
	m_ProcessList.SetItemText(nRet,1,buf);
	
	SecureZeroMemory(buf,MAX_PATH);
	sprintf_s(buf,MAX_PATH,"%d",pi.FileCRC);
	m_ProcessList.SetItemText(nRet,2,buf);


	char chrYesNo[2][10]={"No","Yes"};
	SecureZeroMemory(buf,MAX_PATH);
	sprintf_s(buf,MAX_PATH,"%s",chrYesNo[pi.isConnectNet]);
	m_ProcessList.SetItemText(nRet,3,buf);

	char chrProtocol[2][10]={"TCP","UDP"};
	SecureZeroMemory(buf,MAX_PATH);
	sprintf_s(buf,MAX_PATH,"%s",chrProtocol[pi.protocol]);
	m_ProcessList.SetItemText(nRet,4,buf);
}

void CFireWallDlg::DeleteProcessListItem( ProcInfo pi )
{
	DWORD nCount=m_ProcessList.GetItemCount();
	for (DWORD i=0;i<nCount;i++)
	{
		char buf[MAX_PATH]={0};
		DWORD tmpDw=0;
		m_ProcessList.GetItemText(i,0,buf,MAX_PATH);
		sscanf_s(buf,"%d",&tmpDw);
		if (tmpDw==pi.pid && tmpDw!=0)
		{
			m_ProcessList.DeleteItem(i);
			break;
		}
	}
}
void CFireWallDlg::OnBnClickedBtnNetcontrol()
{
	CAPIHook::BlockNet(!CAPIHook::IsNetBlocked());
}

BOOL CFireWallDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	if (pMsg->message==WMAPIHOOK)
	{
		AfxMessageBox("");
	}
	return __super::PreTranslateMessage(pMsg);
}

LRESULT CFireWallDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	// TODO: Add your specialized code here and/or call the base class
	if (message==WMAPIHOOK)
	{
		AfxMessageBox("");
	}
	return __super::WindowProc(message, wParam, lParam);
}

BOOL CFireWallDlg::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	// TODO: Add your specialized code here and/or call the base class
	if (message==WMAPIHOOK)
	{
		AfxMessageBox("");
	}
	return __super::OnWndMsg(message, wParam, lParam, pResult);
}
